package command;

import util.BookStorage;
import util.Customer;
import util.CustomerStorage;
import util.IO;

public class ReserveBookCommand implements Command {
    public static final String FAIL_NOTIFY = "Sorry we don't have that book yet\n";
    public static final String SUCCESS_NOTIFY = "Thank You!Enjoy the Book.\n";
    private static final String REQUIRE_LOGIN_MESSAGE = "Please login first!\n";
    private int bookId;

    public ReserveBookCommand() {

    }

    public ReserveBookCommand(String bookIdStr) {
        this.bookId = Integer.parseInt(bookIdStr);
    }

    public ReserveBookCommand(int bookId) {
        this.bookId = bookId;
    }


    @Override
    public void run() {
        if (isNullCustomer()) {
            requireLogin();
        } else if (BookStorage.isContain(bookId)) {
            reserve(bookId);
            notifyWithSuccess();
        } else
            notifyWithFail();
    }

    private void requireLogin() {
        IO.output(REQUIRE_LOGIN_MESSAGE);
    }

    private boolean isNullCustomer() {
        Customer customer = CustomerStorage.getLoginCustomer();
        return customer == null;
    }

    private void notifyWithFail() {
        IO.output(FAIL_NOTIFY);
    }

    private void notifyWithSuccess() {
        IO.output(SUCCESS_NOTIFY);
    }

    private void reserve(int bookId) {
        Customer customer = CustomerStorage.getLoginCustomer();
        customer.addBook(BookStorage.get(bookId));
    }
}
